// src/store/userinfo.ts
import { defineStore } from 'pinia'
import { getUserInfo } from '@/api/userinfo.js'
import { ref } from 'vue'

export interface UserInfoState {
    name: string
    sex: string
    identity: string
    imgUrl: string
    department: string
    account: string
    email: string
}

export const useUserinfoStore = defineStore('userinfo', () => {
    // state
    let name = ref('')
    let sex = ref('')
    let identity = ref('')
    let imgUrl = ref('')
    let department = ref('')
    let account = ref('')
    let email = ref('')

    // actions
    async function userInfo(id: string) {
        const res = await getUserInfo(id)
        name.value = res.name
        sex.value = res.sex
        identity.value = res.identity
        imgUrl.value = res.image_url
        department.value = res.department
        account.value = res.account
        email.value = res.email
    }

    // 返回值：暴露状态和方法
    return {
        name,
        sex,
        identity,
        imgUrl,
        department,
        account,
        email,
        userInfo
    }
}, {
    persist: {
        enabled: true,//存储全部字段
        key: 'pinia-userinfo', // 存储的键名
        storage: localStorage  // 默认是 localStorage
    }
})